<?php
require_once('AbstractController.php');
include_once ("arc/ARC2.php");
/**
 * SearchController.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class SearchController extends Thesis_AbstractController
{
	
	public function init(){
		
		$this->_helper->layout->setLayout('thesis');
	}
	
	public function runindexerAction()
	{
		$this->_helper->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	    
	    set_time_limit(0);
	    
	    
	    $this->_indexPublications();
	    $this->_indexAuthors();
	    $this->_indexContinents();
	    
	    exit('finished indexing');
	}
	
	public function indexAction(){
		$query = $this->getRequest()->getParam('q');
		
		$index = Application_Model_Factory_Search_Index::get('Publication');

		$publications = array();
		if(!is_null($query)){
			$publications = $index->getIndex()->find($query);
		}
		
		$index = Application_Model_Factory_Search_Index::get('Author');

		$authors = array();
		if(!is_null($query)){
			$authors = $index->getIndex()->find($query);
		}
		
		$index = Application_Model_Factory_Search_Index::get('Continent');

		$continents = array();
		if(!is_null($query)){
			$continents = $index->getIndex()->find($query);
		}
		
		$this->view->assign('publications', $publications);
		$this->view->assign('authors', $authors);
		$this->view->assign('continents', $continents);
	}
	
	protected function _indexPublications(){
		// Begin indexing publications.
	    $index = Application_Model_Factory_Search_Index::get('Publication');
	    $index->clear();

	    $store = $this->_getStore();
		$query =
			'PREFIX dc: <http://purl.org/dc/elements/1.1/>
  				SELECT ?title FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
				?d dc:title ?title .
   			}';

		$rows = $store->query($query, 'rows');
		foreach ((array) $rows as $row) {
			$index->updateDocument(array(
				'title'	=> $row['title']
		    ));
		}

	    $index->getIndex()->optimize();
	    echo 'finished indexing publications<br />';
	}
	
	protected function _indexAuthors(){
	
	    // Begin indexing authors.
	    $index = Application_Model_Factory_Search_Index::get('Author');
	    $index->clear();
	    
	    $store = $this->_getStore();
		$query = 'select ?name where {
				?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
				?a <http://xmlns.com/foaf/0.1/name> ?name
			} order by ?name';
		
		$rows = $store->query($query, 'rows');
		foreach ((array) $rows as $row) {
			$index->updateDocument(array(
				'name'	=> $row['name']
		    ));
		}
	    $index->getIndex()->optimize();
	    echo 'finished indexing authors<br />';
	}
	
	protected function _indexContinents(){
		
		$store = $this->_getStore();
		$query = 'select ?location where {
		 	?d <http://dbpedia.org/ontology/Place> ?uri .
			?uri <http://www.w3.org/2002/07/owl#sameAs> ?location
		}';
		$rows = $store->query($query, 'rows');
		
		$store = $this->_getStore(8892);
		$locationContinents = array();
		foreach ((array) $rows as $row) {
			
			if(isset($locationContinents[$row['location']])){
				continue;
			}
			
			$query = sprintf(
				'PREFIX gn: <http://www.geonames.org/ontology#>
				select ?name where {
				  {
				      <%s> gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }UNION{
				      <%s> gn:parentFeature ?region_uri .
				      ?region_uri gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }
				  ?continent_uri gn:featureCode ?c .
				  FILTER(regex(?c,"CONT"))
				} ORDER BY ?name',
				$row['location'],
				$row['location']
			);
			$tmpRows = $store->query($query, 'rows');
			if($tmpRows){
				$locationContinents[$row['location']] = $tmpRows[0]['name'];
			}
		}
		
		$index = Application_Model_Factory_Search_Index::get('Continent');
	    $index->clear();
		
		$locationContinents = array_unique($locationContinents);
		foreach($locationContinents as $continent){
			$index->updateDocument(array(
				'title'	=> $continent
		    ));
		}
		
	    $index->getIndex()->optimize();
	    echo 'finished indexing continents<br />';
	}
}